<template>
  <view class="take-list">
    <my-header title="选择需要提货的商品"></my-header>
    <my-content class="content">
      <view
        @click="selected(item, index)"
        class="box-item"
        v-for="(item, index) in orderData"
        :key="index"
        :style="{ backgroundImage: `url(https://www.dingxians.cn/upload/images/box/bg-box-${item.level_id}.png)` }"
      >
        <view class="time" style="visibility: hidden">{{ item.created_at }}</view>
        <view class="goods-info">
          <image class="goods-img" :src="item.blind_box_prize_image"></image>
          <view class="goods-content">
            <view class="goods-name">{{item.blind_box_prize_name}}</view>
            <view class="goods-price">
              ￥{{ item.price }}
              <!--              <text class="goods-num">X{{ item.num }}</text>-->
              <my-number-box :show="item.num > 1" v-model="item.num" :max="item.num" :min="1"></my-number-box>
            </view>
            <view class="goods-recovery-price">拒提价：{{ item.recovery_price }}</view>
            <!--            <view class="goods-tips" v-if="!item.expired && item.status === 1">
                          请在
                          <text class="box_tip_time">{{ item.expired_at }}</text>
                          前提货，过期将只能拒提
                        </view>-->
            <view class="goods-tips timeout" v-if="item.expired">
              <view>已超时无法提货商品</view>
            </view>
            <view class="goods-tips" v-if="item.status === 2">赠送时间：{{ item.give_at }}</view>
            <view class="goods-tips" v-if="item.status === 3">拒提时间：{{ item.exchange_at }}</view>
            <view class="goods-tips" v-if="item.status === 4">反悔时间：{{ item.back_at }}</view>
            <view class="goods-tips" v-if="item.status === 5">提货时间：{{ item.updated_at }}</view>
          </view>
        </view>
        <view class="goods-bottom">
          <view class="goods-title">{{ item.blind_box_name }} [{{ item.resource }}]</view>
          <view class="create-at">{{ item.created_at }}</view>
        </view>

        <image
          class="select"
          :src="'https://www.dingxians.cn/upload/images/common/' + (item.selected ? 'selected' : 'unselected') + '.png'"
        ></image>
      </view>
      <view style="visibility: hidden;">1</view>
      <view class="freight">
        <view class="freight_title"><!-- 单次提货满3件及以上包邮（偏远地区除外） --></view>
        <view class="freight_rule" @click="header('/packageB/content/freight')">运费规则 ></view>
      </view>
      <view class="bottom">
        <view class="bottom_select" @click="allQuery()">
          <image class="bottom_select_icon" src="https://www.dingxians.cn/upload/images/common/unselected.png" v-if="!allSelected"></image>
          <image class="bottom_select_icon" src="https://www.dingxians.cn/upload/images/common/selected.png" v-if="allSelected"></image>
          <view style="font-size: 24rpx;">全选</view>
          <view class="bottom_text">
            <view>已选</view>
            <view class="bottom_text_num">{{ num }}</view>
            <view class="bottom_txt">件</view>
          </view>
        </view>

        <view class="exchange_btn btn-primary" v-if="num == 0">立即提货</view>
        <view class="exchange_btn btn-primary exchange" v-else @click="take()">立即提货</view>
      </view>
    </my-content>
  </view>
</template>

<script>
import api from '../../api/index.js'
import box from '@/mixins/box'

export default {
  mixins: [box],
  data() {
    return {
      orderData: [],
      id: '',
      allSelected: false,
      page: 1,
      lastPage: 1,
      type: 0,
    }
  },
  onLoad(option) {
    this.id = option.id
    this.type = +option.type
    this.getGoods()
  },
  onShow() {
  },

  onReachBottom() {
    if (this.page > this.lastPage) {
      this.show('已经到底了')
      return
    }
    this.getGoods()
  },

  computed: {
    // 选中的商品
    num() {
      let total_num = 0
      this.orderData.filter(item => {
        return item.selected == true
      }).forEach((item) => {
        total_num += item.num
      })
      return total_num
    },
  },
  methods: {
    splitNum(num) {
      // if (typeof(num) == 'number') {
      return num.toString().split('.')
      // }
      // return num.split('.')

    },
    allQuery() {
      this.allSelected = !this.allSelected

      for (let i = 0; i < this.orderData.length; i++) {
        this.orderData[i].selected = this.allSelected
        this.$set(this.orderData, i, this.orderData[i])
      }
    },
    selected(item, index) {
      item.selected = !item.selected
      this.$set(this.orderData, index, item)
    },
    getGoods() {
      uni.showLoading({
        title: '数据加载中',
      })
      let promise
      if (this.type === 1) {
        promise = api.welfareHouses.takeShow({ page: this.page })
      } else if (this.type === 0) {
        promise = api.blindBoxOrderPrizes.takeShow({ page: this.page })
      } else if (this.type === 2) {
        promise = api.towers.takeShow({page: this.page})
      } else if (this.type === 3) {
        promise = api.compounds.takeShow({page: this.page})
      }
      promise.then(res => {
        if (res.code === 200) {
          uni.hideLoading()
          this.orderData = this.orderData.concat(res.data.data)
          this.lastPage = res.data.lastPage
          this.page++
        }
      }).catch(error => {
        console.log(error)
      })
    },
    take() {
      let ids = this.orderData.filter(item => {
        return item.selected == true
      }).map(item => {
        return item.id
      })
      if (ids.length > 100) {
        this.show('一次最多提货100件')
        return
      }
      ids = JSON.stringify(ids)
      console.log(ids)
      this.header(`/pages/box/order?ids=${ids}&type=` + this.type)
    },
  },
}
</script>

<style lang="scss">
.take-list {
  min-height: 100vh;
  height: 100%;
  // background-color: #f7f7f7;
  background: #141414;
}
.content {
  min-height: 100vh;
  height: 100%;
  // background-color: #f7f7f7;
  background: #141414;

  .box-item {
    margin: 0 auto 0 auto;
    width: 710rpx;
    height: 460rpx;
    background-size: 100% 100%;
    color: white;
    box-sizing: border-box;
    padding: 50rpx 50rpx;
    position: relative;

    .create-at {
      font-size: 24rpx;
      text-align: right;
      margin-right: 20rpx;
    }

    .goods-img {
      width: 160rpx;
      height: 160rpx;
      margin-left: 31rpx;
      margin-top: 50rpx;
    }

    .goods-info {
      margin-top: 20rpx;
      display: flex;

      .goods-content {
        flex: 1;
        margin-left: 30rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        .goods-name {
          width: 400rpx;
          margin-top: 30rpx;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 28rpx;
        }

        .goods-price {
          font-size: 40rpx;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .goods-num {
            font-size: 24rpx;
          }
        }

        .goods-recovery-price {
          font-size: 22rpx;
        }

        .goods-tips {
          font-size: 22rpx;
        }

        .timeout {
          color: red;
        }
      }
    }

    .select {
      width: 32rpx;
      height: 32rpx;
      position: absolute;
      right: 60rpx;
      top: 60rpx;
    }

    .goods-bottom {
      margin-top: 40rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-title {
        font-size: 24rpx;
        margin-left: 20rpx;
      }

      .goods-control {
        .action-btn {
          margin-right: 20rpx;
          width: 100rpx;
          height: 46rpx;
        }
      }
    }
  }

  .freight {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 120rpx;
    left: 0;
    width: 100%;
    height: 50rpx;
    align-items: center;
    background-color: #030522;
    font-size: 24rpx;

    .freight_title {
      margin-left: 30rpx;
    }

    .freight_rule {
      margin-right: 30rpx;
      color: #ee5093;
    }
  }

  .bottom {
    display: flex;
    justify-content: space-between;
    background-color: #030522;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 120rpx;
    align-items: center;
    color: white;

    .bottom_select {
      display: flex;
      align-items: center;

      .bottom_select_icon {
        width: 32rpx;
        height: 32rpx;
        margin: 0 20rpx;
      }
    }

    .bottom_text {
      display: flex;
      align-items: center;
      font-weight: normal;
      font-size: 24rpx;
      margin-left: 70rpx;

      .bottom_text_num {
        margin: 0 10rpx;
        color: #ee5093;
        font-size: 24rpx;
      }
    }

    .exchange_btn {
      height: 84rpx;
      line-height: 84rpx;
      text-align: center;
      width: 228rpx;
      margin: 0 20rpx;
      font-size: 28rpx;
      background-size: 100% 100%;
      filter: grayscale(1);

      &.exchange {
        filter: none;
        color: #fff;
      }
    }
  }

}</style>